﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElectronicCommerce.Framework.Web.Configuration;
using ElectronicCommerce.Framework.Web.Configuration.Resource;

namespace ElectronicCommerce.Framework.Web.UI
{
    public class PageFoot
    {
        private List<JavascriptItem> javascriptFiles = null;
        private List<JavascriptItem> javascriptInlines = null;

        public PageFoot()
        {
            JavascriptList = new List<JavascriptItem>();
        }

        public List<JavascriptItem> JavascriptList { get; private set; }

        #region Javascript

        public void AddJavascriptFile(Enum jsAlias)
        {
            Javascript js = ConfigHelper.JavascriptConfig.GetJavascript(jsAlias.ToString());
            AddJavascriptFile(js.FileName);
        }

        public void AddJavascriptFile(Enum jsAlias, string param)
        {
            Javascript js = ConfigHelper.JavascriptConfig.GetJavascript(jsAlias.ToString());
            AddJavascriptFile(js.FileName + "?" + param);
        }

        public void AddJavascriptFile(string javascriptFile)
        {
            AddJavascriptItem(JavascriptType.External, javascriptFile);
        }

        public void AddInlineJavascript(string content)
        {
            AddJavascriptItem(JavascriptType.Inline, content);
        }

        public virtual void AddJavascriptItem(JavascriptType javascriptType, string content)
        {
            JavascriptItem javascriptItem = new JavascriptItem();
            javascriptItem.Type = javascriptType;
            javascriptItem.Content = content;
            if (!JavascriptList.Contains(javascriptItem))
            {
                JavascriptList.Add(javascriptItem);
            }
        }

        public List<JavascriptItem> GetJavascriptFiles()
        {
            EnsureJavascriptSplited();
            return javascriptFiles;
        }

        public List<JavascriptItem> GetInlineJavascripts()
        {
            EnsureJavascriptSplited();
            return javascriptInlines;
        }

        private void EnsureJavascriptSplited()
        {
            if (javascriptFiles == null || javascriptInlines == null)
            {
                javascriptFiles = new List<JavascriptItem>();
                javascriptInlines = new List<JavascriptItem>();
                foreach (JavascriptItem javascriptItem in JavascriptList)
                {
                    if (javascriptItem.Type == JavascriptType.Inline)
                    {
                        javascriptInlines.Add(javascriptItem);
                    }
                    else if (javascriptItem.Type == JavascriptType.External)
                    {
                        javascriptFiles.Add(javascriptItem);
                    }
                    else if (javascriptItem.Type == JavascriptType.ExternalOtherWeb)
                    {
                        javascriptFiles.Add(javascriptItem);
                    }
                }
            }
        }

        #endregion
    }
}
